package gold.digger;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC189 {

//
//    Input: nums = [1,2,3,4,5,6,7], k = 3
//    Output: [5,6,7,1,2,3,4]
//    Explanation:
//    rotate 1 steps to the right: [7,1,2,3,4,5,6]
//    rotate 2 steps to the right: [6,7,1,2,3,4,5]
//    rotate 3 steps to the right: [5,6,7,1,2,3,4]

    public void rotate(int[] arr, int k) {
        if (null == arr || arr.length <= 1) {
            return;
        }

        if (arr.length < k) {
            k -= arr.length;
        }

        int[] copy = new int[k];
        for (int i = 0; i < k; i++) {
            copy[i] = arr[arr.length - k + i];
        }

        for (int i = arr.length - k - 1; i >= 0; i--) {
            arr[i + k] = arr[i];
        }

        for (int i = 0; i < k; i++) {
            arr[i] = copy[i];
        }
    }


    public static void main(String[] args) {
        LC189 an = new LC189();
        int[] arr = {1, 2};
        an.rotate(arr, 3);
    }
}
